<?php 
error_reporting(E_ERROR);
session_start();
require_once('constants.php');
require_once('functions.php');
include('classes/userClass.php');



	// get user data
$userObj = new userClass();

$params=array($_SESSION['session_user']);
$row = $userObj->fetch_result("SELECT * FROM user WHERE session_user=?",$params);
$user = $row[0];

//get shop details by name
if(isset($_SESSION['shop_creation'])){
	$params=array($_SESSION['shop_creation']);
	$row = $userObj->fetch_result("SELECT * FROM user_shops WHERE id=?",$params);
	$shop = $row[0];
	// print_r($shop);
}


if(isset($_POST['site_name'])){
	$params=array($_POST['site_name']);
	$row = $userObj->fetch_result("SELECT * FROM user_shops WHERE name=?",$params);
	$shop = $row[0];
	// print_r($shop);
}
if($shop){
	$params['hostname']=DB_SERVER;
	$params['dbname']='neoflux_'.$shop['db_name'];
	$params['username']=$shop['db_user'];
	$params['password']=$shop['db_pass'];
	#conect to shop database	
	$shopObj = new mysqlClass($params);
}
if($_SESSION['shop_creation']==0){
		// test if shop name already exist in database
	
	if(count($shop)==0){

			//good to create shop
		echo "create shop";

	}else if($shop['user_id']==$user['id']){

			//you already have a shop with this name
		echo "you already have a shop with this name";

	}else{

			// this name is already used
		echo "this name is already used";

	}
}else if($_SESSION['shop_creation']!=0 && isset($_POST['languages'])){

		//update current shop

	//set params for connection to shop database
	$params =array($_POST['domain_name']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='domain_name'",$params);
	$params =array($_POST['email_contact']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='contact_email'",$params);
	$params =array($_POST['telephone']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='contact_telephone'",$params);
	$params =array($_POST['address']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='contact_address'",$params);
	$params =array($_POST['zip_code']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='zip_code'",$params);
	$params =array($_POST['city']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='city'",$params);
	$params =array($_POST['state']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='country'",$params);
	$params =array($_POST['map_marker']);
	$shopObj->execute_query("UPDATE settings SET value=? WHERE name='map_location'",$params);
	
	//update shop languages
	$langs = explode(",", $_POST['languages']);
	$shopObj->execute_query("UPDATE languages SET active='0'");
	foreach ($langs as $key => $value) {
		$params=array($value);
		$shopObj->execute_query("UPDATE languages SET active='1' WHERE abv=?",$params);
	}
	// update shop categories
	$categories = explode(",", $_POST['site_category']);
	$userObj->execute_query("DELETE FROM shop_categories WHERE shop_id='{$shop['id']}'");
	foreach ($categories as $key => $value) {
		$params= array();
		$params['shop_id'] = $shop['id']; 
		$params['category_id'] = $value; 		
		$userObj->insert('shop_categories',$params);
	}
	

}
if(isset($_POST['update_shop'])){

	

	$params = array('value'=>$_POST['pick_theme'],'id'=>'5');
	$shopObj->update('settings',$params);



	// $params = array('value'=>$_POST['pick_product_list'],'id'=>'13');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['compare_prod_check'],'id'=>'14');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['side_bar_check'],'id'=>'15');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['newsletter_check'],'id'=>'16');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['product_number'],'id'=>'17');
	// $shopObj->update('settings',$params);
	
	// $params = array('value'=>$_POST['pagination'],'id'=>'18');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['pick_product_page'],'id'=>'19');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['rating_check'],'id'=>'20');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['share_facebook_check'],'id'=>'21');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['share_tweeter_check'],'id'=>'22');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['comments'],'id'=>'23');
	// $shopObj->update('settings',$params);

	// $params = array('value'=>$_POST['fqs_check'],'id'=>'24');
	// $shopObj->update('settings',$params);

	
	
}
if(isset($_POST['update_theme'])){



	$params = array('value'=>$_POST['update_theme'],'id'=>'5');
	$shopObj->update('settings',$params);

	$params=array($_POST['update_theme']);
	$theme_id_row = $userObj->fetch_result('SELECT id FROM themes WHERE name=? LIMIT 1',$params);
	$theme_id=$theme_id_row[0]['id'];
	$color_scheme_rows = $userObj->fetch_result("SELECT * FROM color_scheme WHERE theme_id={$theme_id}");
	$i=0;
	$data="<div class='row-fluid' style='margin-bottom:30px'>";
	foreach ($color_scheme_rows as $key => $value) {
		
		if($i%3==0){
			$data.="</div>
			<div class='row-fluid' style='margin-bottom:30px'>";
		};
		$colors = explode(",", $value['values']);
		$data.="<div class='span4 centered-text";
		if($value['default']==1){
			$data.=' selected_color_scheme';
		}
		$data.="'>
		<div class='color_square_holder'>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='background' style='background-color:#{$colors[0]};'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='main-color' style='background-color:#{$colors[1]}'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='body-font-color' style='background-color:#{$colors[2]}'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='menu-and-title-bg header si hedinguri' style='background-color:#{$colors[3]}'></div>
		<br class='clear'/>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='alt-header-color' style='background-color:#{$colors[4]}'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='body-link-color' style='background-color:#{$colors[5]}'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='menu-link-color' style='background-color:#{$colors[6]}'></div>
		<div class='color_square tooltip-info' data-rel='tooltip' data-placement='top' data-original-title='footer-and-breadcrumbs' style='background-color:#{$colors[7]}'></div>
		<br class='clear'/>
		</div>";
		if($value['default']==1){	
			$data.='<script>selected_color_scheme='.$value['id'].';close_spiner();</script>';
			$data.="<button class='btn btn-small btn-success' onclick ='select_color_scheme({$value['id']},this);return false;' >
			<i class='icon-check'></i>
			<span>Selected</span>
			</button>";
		}else{
			$data.="<button class='btn btn-small btn-grey' onclick ='select_color_scheme({$value['id']},this);return false;'>
			<i class='icon-check-empty'></i>
			<span>Unselected</span>
			</button>";
		}	
		$data.=						"<button class='btn btn-small btn-purple' onclick='preview_theme(\"{$value['values']}\");return false;'>
		<i class='icon-desktop'></i>
		Preview
		</button>						
		</div>";

		$i++;
	}
	$data.='</div>';
	echo $data;
}
if(isset($_POST['get_table_categories'])){
	#set params
	

	#get active languages\
	$params=array('1');
	$langs=$shopObj->fetch_result("SELECT id,abv FROM languages WHERE active=? ORDER BY id ",$params);

	$return_data='<table id="sample-table-2" class="table table-striped table-bordered table-hover">
	<thead>
	<tr>';
	foreach($langs as $key=>$value){
		$return_data.= '<th>category '.$value['abv'].'</th>';
		$language_arr[]='language_id='.$value['id'];
		$all_langs[$value['id']]=$value['abv'];
	}
	$return_data.='	<th>Parent</th>	
	<th>Show in footer</th>
	<th>Actions</th>
	</tr>
	</thead>
	<tbody>';
	#mini query lang
	$langs_query = implode(' OR ', $language_arr );

	#define options for parent select
	$options[0]="no parent";	
	$categories = $shopObj->fetch_result("SELECT * FROM categories");
	foreach ($categories as $key => $value) {
		$catname = $shopObj->fetch_result("SELECT name FROM category_details WHERE category_id={$value['id']} AND language_id={$langs[0]['id']}");
		if($value['parent_id']==0){
			$options[$value['id']]=$catname[0]['name'];
		}
		
	}

	#list all categories
	foreach ($categories as $key => $value) {
		$return_data.='<tr id="category_'.$value['id'].'">';
		$category_desc=$shopObj->fetch_result("SELECT name,language_id FROM category_details WHERE category_id={$value['id']} AND ( {$langs_query} ) ORDER BY language_id");
		foreach ($category_desc as $key1 => $value1) {
			$return_data.='<td><a href="#modal-table" role="button"  data-toggle="modal" onclick="populate_category('.$value['id'].',\''.$all_langs[$value1['language_id']].'\')">'.$value1['name'].'</a></td>';
		}	
		// if($value['parent_id']!=0){
		// 	$parent=$shopObj->fetch_result("SELECT name FROM category_details WHERE category_id={$value['parent_id']}  ORDER BY language_id LIMIT 1");
		// 	$parent_name=$parent[0]['name'];
		// }else{
		// 	$parent_name='';
		// }		
		$return_data.='	<td><select onchange="change_parent(this.value,'.$value['id'].')">';
		foreach($options as $key2=>$value2){
			if($key2==$value['parent_id']){
				$selected = 'selected';
			}else{
				$selected='';
			}
			$return_data.="<option {$selected} value='{$key2}'>{$value2}</option>";
		}
		$return_data.="</select></td>";
		
		if($value['show_in_footer']==1){
			$checked="checked='checked'";
		}else{
			$checked='';
		}

		$return_data.="<td><input type='checkbox' ".$checked." onclick='show_in_footer(".$value['id'].")'>
							<span class='lbl'> </span>
						</td>";
		$return_data.='<td class="td-actions">
		<div class="hidden-phone visible-desktop action-buttons">


		<a href="#modal-table" role="button" class="green" data-toggle="modal" onclick="populate_category('.$value['id'].')">
		<i class="icon-pencil "></i>
		</a>

		<a class="red" href="javascript:void(0)" onclick="delete_category('.$value['id'].')">
		<i class="icon-trash "></i>
		</a>



		</div>

		<div class="hidden-desktop visible-phone">
		<div class="inline position-relative">
		<button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown">
		<i class="icon-caret-down icon-only bigger-120"></i>
		</button>

		<ul class="dropdown-menu dropdown-icon-only dropdown-yellow pull-right dropdown-caret dropdown-close">
		<li>
		<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
		<span class="blue">
		<i class="icon-zoom-in bigger-120"></i>
		</span>
		</a>
		</li>

		<li>
		<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
		<span class="green">
		<i class="icon-edit bigger-120"></i>
		</span>
		</a>
		</li>

		<li>
		<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
		<span class="red">
		<i class="icon-trash bigger-120"></i>
		</span>
		</a>
		</li>
		</ul>
		</div>
		</div>
		</td>';
		$return_data.='</tr>';
	}
	$return_data.='</tbody>
	</table>';
	echo $return_data;
}	
if(isset($_POST['save_color_scheme']) || isset($_POST['save_custom_color_scheme'])){
	if(isset($_POST['save_color_scheme'])){
		$params=array($_POST['save_color_scheme']);
		$row_colors = $userObj->fetch_result("SELECT `values` FROM color_scheme WHERE id=?",$params);
		$colors = explode(",", $row_colors[0]['values']);
	}
	if(isset($_POST['save_custom_color_scheme'])){
		$colors = explode(",", $_POST['save_custom_color_scheme']);

	}
	

	foreach ($colors as $key => $value) {
		$colors[$key] = str_replace("#", '', $value);
	}
	
	$params=array('value'=>$colors[0],'id'=>28);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[1],'id'=>29);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[2],'id'=>30);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[3],'id'=>31);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[4],'id'=>32);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[5],'id'=>33);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[6],'id'=>34);
	$shopObj-> update('settings',$params);

	$params=array('value'=>$colors[7],'id'=>35);
	$shopObj-> update('settings',$params);

}

if(isset($_POST['delete_category'])){
	
	#delete from categories properties
	$params=array();
	$params['category_id']=$_POST['delete_category'];
	$shopObj->delete('categories_properties',$params);
	#delete from category_details
	$params=array();
	$params['category_id']=$_POST['delete_category'];
	$shopObj->delete('category_details',$params);
	#delete from categories
	$params=array();
	$params['id']=$_POST['delete_category'];
	$shopObj->delete('categories',$params);
}
if(isset($_POST['populate_category'])){
	

	$params=array($_POST['populate_category']);
	$categories = $shopObj->fetch_result("SELECT * FROM category_details WHERE category_id=?",$params);
	
	foreach($categories as $key=>$value){
		$data[$key]['id']=$value['id'];
		$data[$key]['category_name_'.$value['language_id']]=$value['name'];
		$data[$key]['category_url_'.$value['language_id']]=$value['url'];
		$data[$key]['category_meta_keywords_'.$value['language_id']]=$value['meta_keywords'];
		$data[$key]['category_meta_description_'.$value['language_id']]=$value['meta_description'];
		$data[$key]['category_description_'.$value['language_id']]=$value['description'];
	}
	$prop_types=array('1'=>'normal','2'=>'color','3'=>'picture','4'=>'icon','5'=>'size');
	$langs = $shopObj->fetch_result("SELECT abv,id FROM languages WHERE active=1 ");
	foreach ($langs as $value ){
		$properties =$shopObj->fetch_result("SELECT 
												properties.*,properties_details.name 
											FROM 
												properties_details,properties,categories_properties
											WHERE 
												properties_details.property_id = properties.id 
											AND 
												properties_details.language_id={$value['id']} 
											AND 
												categories_properties.property_id =properties.id
											AND 
												categories_properties.category_id='{$_POST['populate_category']}'   
											");
		foreach($properties as $value_prop){
			$table[$value['abv']].="<tr>
								<td>{$value_prop['name']}</td>
								<td><select>";
						foreach($prop_types as $key=> $value_types){
							echo "<option value={$key}";
							if($key==$value_prop['type']){
								echo " selected='selected' ";
							}
							echo ">{$value_types}</option>";
						}
			$table[$value['abv']].= 	"</select></td>
								<td>
									<a href='#'>
										<i class='icon-pencil '></i>
									</a>

									<a class='red' href='javascript:void(0)' >
										<i class='icon-trash '></i>
									</a>
								</td>
							</tr>";
		}
		$data['table'][$value['abv']]=$table[$value['abv']];
							
	}

									
	echo json_encode($data);
}
if(isset($_POST['save_category'])){

	foreach($_POST as $key=>$value){
		if($key!='save_category'){

			$key = str_replace("category_", '', $key);
			$lang_id = substr($key, -1);  
			$key = substr($key, 0, -2);
			if($key=='name'){
				$url=createURL('category_details',$value,$shopObj );				
			}
			if($key=='url'){
				$data[$lang_id]['url']=$url;
				
			}else{
				$data[$lang_id][$key]=$value;
			}
		}
	}
	foreach ($data as $key => $value) {
		$params=array();
		foreach ($value as $key1 => $value1) {
			$params[]=$value1;
		}
		$params[]=$key;
		$params[]=$_POST['save_category'];	

		$shopObj->execute_query("UPDATE category_details SET name=?,url=?,	description=?,meta_description=?,meta_keywords=? WHERE language_id=? AND category_id=?",$params);
	}

		#return data for table update
		#get active langs
	$params=array('1');
	$langs=$shopObj->fetch_result("SELECT id,abv FROM languages WHERE active=? ORDER BY id ",$params);
		#make query piece
	foreach($langs as $key=>$value){		
		$cat_name[] = '<a href="#modal-table" role="button"  data-toggle="modal" onclick="populate_category('.$_POST['save_category'].',\''.$value['abv'].'\')">'.$data[$value['id']]['name'].'</a>';
	}

	echo json_encode($cat_name);
}
//chage checkboxex
if(isset($_POST['change_checkbox'])){
	if(isset($_POST['compare_prod_check'])){
		$params=array('value'=>$_POST['compare_prod_check'],'id'=>14);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['side_bar_check'])){
		$params=array('value'=>$_POST['side_bar_check'],'id'=>15);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['newsletter_check'])){
		$params=array('value'=>$_POST['newsletter_check'],'id'=>16);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['color_on_image'])){
		$params=array('value'=>$_POST['color_on_image'],'id'=>27);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['rating_check'])){
		$params=array('value'=>$_POST['rating_check'],'id'=>20);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['comments_check'])){
		$params=array('value'=>$_POST['comments_check'],'id'=>23);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['fqs_check'])){
		$params=array('value'=>$_POST['fqs_check'],'id'=>24);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['fqs_check'])){
		$params=array('value'=>$_POST['fqs_check'],'id'=>21);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['fqs_check'])){
		$params=array('value'=>$_POST['fqs_check'],'id'=>22);
		$shopObj-> update('settings',$params);
	}
}
if(isset($_POST['save_radio'])){
	if(isset($_POST['share_facebook_check'])){
		$params=array('value'=>$_POST['share_facebook_check'],'id'=>17);
		$shopObj-> update('settings',$params);
	}
	if(isset($_POST['share_tweeter_check'])){
		$params=array('value'=>$_POST['share_tweeter_check'],'id'=>18);
		$shopObj-> update('settings',$params);
	}
}
if(isset($_POST['save_nr_prod'])){
	$params=array('value'=>$_POST['save_nr_prod'],'id'=>41);
		$shopObj-> update('settings',$params);
		
}
if(isset($_POST['save_input'])){
	$params=array('value'=>$_POST['minim_order'],'id'=>50);
		$shopObj-> update('settings',$params);
		
}
if(isset($_POST['save_currency'])){
	$params=array('value'=>$_POST['save_currency'],'id'=>26);
		$shopObj-> update('settings',$params);
		
}
if(isset($_POST['save_input_tk'])){
	$params=array('value'=>$_POST['save_input_tk'],'id'=>56);
		$shopObj-> update('settings',$params);
		
}
?>
